package com.cryann.mqttserver.mqttProcess;

import com.cryann.mqttserver.modle.Accessor;
import com.cryann.mqttserver.repository.AccessorRepository;
import com.cryann.mqttserver.service.MqttAccessor;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author cRyann
 * @date 2018/12/18 0018 11:17
 * @description:
 */
@Component
@Slf4j
@ProcessType(MqttMessageType.DISCONNECT)
public class DisConnect implements BasicProcess<MqttMessage> {
    @Autowired
    MqttAccessor mqttAccessor;
    @Autowired
    AccessorRepository accessorRepository;

    @Override
    public void process(Channel ctx, MqttMessage msg) {
        Accessor accessor = accessorRepository.findByChannelId(ctx.id().asShortText());
        mqttAccessor.willMessageSend(accessor.getIdentifier());
        mqttAccessor.delAccessor(ctx);
        ctx.disconnect();
        // TODO Will process
    }
}
